#include "stdafx.h"
#include "resource.h"
#include "EmbededBrowserSink.h"
#include "ZEmbededBrowser.h"
#include "EmbededBrowserUI.h"
#include "EmbededBrowserUIHandler.h"

BOOL ZEmbededBrowser::HitOnButtonBlue()
{
	DWORD dwPos = GetMessagePos();
	int xPos = GET_X_LPARAM(dwPos);
	int yPos = GET_Y_LPARAM(dwPos);
	CPoint pt(xPos,yPos);
	m_Toolbar->ScreenToClient(&pt);
	int uIndex = m_Toolbar->CommandToIndex(ID_BUTTON_BLUE);
	return uIndex == m_Toolbar->HitTest(&pt);
}
void ZEmbededBrowser::HideBrowser()
{

	DWORD dwPos = GetMessagePos();
	int xPos = GET_X_LPARAM(dwPos);
	int yPos = GET_Y_LPARAM(dwPos);
	CPoint pt(xPos,yPos);
	m_Toolbar->ScreenToClient(&pt);

	int uIndex = m_Toolbar->CommandToIndex(ID_BUTTON_BLUE);
	int uIndexHit = m_Toolbar->HitTest(&pt);
	CString str;
	str.Format("uIndex %d , uIndexHit , %d\n",uIndex,uIndexHit);
	OutputDebugString(str);
	if( uIndex !=uIndexHit){
		int State = m_Toolbar->GetState(ID_BUTTON_BLUE);
		m_Toolbar->SetState(ID_BUTTON_BLUE,State ^ TBSTATE_CHECKED);
	}
	ShowWindow(SW_HIDE);
}
LRESULT ZEmbededBrowser::OnSetFocus(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
	CString str;
	HWND hExplorer = FindWindowEx(m_hWnd,NULL,_T("Internet Explorer_Server"),NULL);
	HWND hView ;
	m_bHasFocus = TRUE;
	if( hExplorer != NULL ){
	}else{
		hView = FindWindowEx(m_hWnd,NULL,_T("Shell DocObject View"),NULL);		
	}
	str.Format("OnSetFocus self is %x , hView %x ,hExplorer %x , Focused %x\n",m_hWnd,hView,hExplorer,::GetFocus());
	OutputDebugString(str);
	return 0;
}
LRESULT ZEmbededBrowser::OnKillFocus(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
	CString str;
	HWND hExplorer = FindWindowEx(m_hWnd,NULL,_T("Internet Explorer_Server"),NULL);
	HWND hView = FindWindowEx(m_hWnd,NULL,_T("Shell DocObject View"),NULL);
	str.Format("OnKillFocus self is %x , hView %x ,hExplorer %x\n",m_hWnd,hView,hExplorer);
	OutputDebugString(str);
	m_bHasFocus = FALSE;
	if( m_bDocumentCompleted){
		//HideBrowser();
	}
	return 0;
}
LRESULT ZEmbededBrowser::OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
	HideBrowser();
	bHandled =FALSE;
	return 0;
}
LRESULT ZEmbededBrowser::OnClick(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& bHandled)
{
	int xPos = GET_X_LPARAM(lParam);
	int yPos = GET_Y_LPARAM(lParam);
	CRect rc;
	CPoint pt(xPos,yPos);
	GetWindowRect(&rc);
	ClientToScreen(&pt);
	if( !rc.PtInRect(pt)){
		HideBrowser();
	}
	return 0;
}
int toutf8(CComBSTR& src , LPSTR* dest )
{
	int nLen = WideCharToMultiByte(CP_UTF8, 0,src, -1, NULL, NULL,0,0);
	LPSTR lpszUTF8 = new CHAR[nLen];
	WideCharToMultiByte(CP_UTF8, 0, src, -1, lpszUTF8, nLen,0,0);
	* dest = lpszUTF8;
	return nLen;
}

LRESULT ZEmbededBrowser::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
	CAxWindow wnd(m_hWnd);
	CStringW url ;
	LPSTR lpUTF8;
	CComBSTR bstrRequestURL =L"http://192.168.1.86:3000/extension/ie?";
	
	int iLen = toutf8(m_bstrCurrentUrl,&lpUTF8);

	DWORD dwLen=0;
	AtlEscapeUrl(lpUTF8,(LPSTR)&dwLen,&dwLen,0);
	LPSTR lpEscapeBuffer =new CHAR[dwLen];
	BOOL bRet = AtlEscapeUrl(lpUTF8,lpEscapeBuffer,&dwLen,dwLen);
	ATLASSERT(bRet);
	CStringA strCurrentURL=lpEscapeBuffer;
	delete lpUTF8;
	delete lpEscapeBuffer;	


	iLen = toutf8(m_bstrTitle,&lpUTF8);

	dwLen=0;
	AtlEscapeUrl(lpUTF8,(LPSTR)&dwLen,&dwLen,0);
	lpEscapeBuffer =new CHAR[dwLen];
	bRet = AtlEscapeUrl(lpUTF8,lpEscapeBuffer,&dwLen,dwLen);
	ATLASSERT(bRet);
	CStringA strTitle=lpEscapeBuffer;
	delete lpUTF8;
	delete lpEscapeBuffer;	
	CStringA strTemp ;
	strTemp.Format("url=%s&title=%s",strCurrentURL,strTitle);
	CComBSTR bstrQuery=strTemp;
	bstrRequestURL.AppendBSTR(bstrQuery);
	

	LPUNKNOWN pUnkCtrl, pUnkCont;
	HRESULT hr ;
	CComPtr<IWebBrowser2>   m_pBrowserApp;
	hr = CoCreateInstance(CLSID_WebBrowser, NULL, CLSCTX_ALL, IID_IUnknown,	(void**)&pUnkCtrl);
	wnd.AttachControl(pUnkCtrl, &pUnkCont); 
	pUnkCtrl->QueryInterface(IID_IWebBrowser2, (void**) &m_pBrowserApp);

	if( SUCCEEDED(hr)){
		CComObject<CEmbededBrowserUIHandler> *pUIH =NULL;
		hr = CComObject<CEmbededBrowserUIHandler>::CreateInstance(&pUIH);
		if(SUCCEEDED(hr)){
			CComObject<CEmbededBrowserUI> *pUI;

			hr = CComObject<CEmbededBrowserUI>::CreateInstance(&pUI);
			if(SUCCEEDED(hr)){
				CComQIPtr<IDispatch> spDisp=pUI;
				pUIH->m_spExtDispatch = spDisp;

				CComQIPtr<IDocHostUIHandlerDispatch> pIUIH = pUIH;
				CComPtr<IUnknown> spHost;
				hr = wnd.QueryHost(IID_IUnknown,(void**)&spHost);
				if( SUCCEEDED(hr)){ 
					m_spHost=spHost;
					hr = m_spHost->SetExternalUIHandler(pIUIH);
				}
				hr  = wnd.QueryControl(IID_IWebBrowser2,(void**)&m_spWebBrowser);
				// Connect to the WebBrowser
				if( SUCCEEDED(hr)){
					CComObject<CEmbededBrowserSink> *pObj =NULL;
					hr = CComObject<CEmbededBrowserSink>::CreateInstance(&pObj);
					if( SUCCEEDED(hr)){
						hr = pObj->QueryInterface(IID_IUnknown,(void**)&m_spUnkSink);
						if( SUCCEEDED(hr)){
							hr = AtlAdvise(m_spWebBrowser,m_spUnkSink,DIID_DWebBrowserEvents2,&m_dwSinkCookie);
							if( SUCCEEDED(hr)){
								pObj->m_spWebBrowser = m_spWebBrowser;
								pObj->m_winBrowserHost = this;
								m_spWebBrowser->Navigate(bstrRequestURL,0,0,0,0);
							} else {
								ATLTRACE("AtlAdvise Failed , HRESULT %x\n",hr);
							}
						}
					}
					
				}
			}
		}
	}
	return SUCCEEDED(hr) ? 0 :-1;
}
ZEmbededBrowser::~ZEmbededBrowser()
{
	HRESULT hr;
	if( m_spWebBrowser ){
		hr = AtlUnadvise(m_spWebBrowser,DIID_DWebBrowserEvents2,m_dwSinkCookie);
		ATLASSERT(SUCCEEDED(hr));
	}
	if( m_hWnd && ::IsWindow(m_hWnd)){
		DestroyWindow();
	}
}